﻿using IntelligentAgriculture.Domain.Entity;
using IntelligentAgriculture.Domain.Interface;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntelligentAgriculture.Domain.Impl
{
    public class BreedingWeighRepository:IBreedingWeighRepository
    {

        /// <summary>
        /// 上下文
        /// </summary>
        private readonly AgricultureDbContext context;
        public BreedingWeighRepository(AgricultureDbContext context)
        {
            this.context = context;
        }


        /// <summary>
        /// 获取称重数据
        /// </summary>
        /// <returns></returns>
        public async Task<IQueryable<Breedingweigh>> BreedingWeigh()
        {
            return context.Breedingweighs;

        }
        /// <summary>
        /// 获取称重数据列表
        /// </summary>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="earTag"></param>
        /// <param name="weighCategory"></param>
        /// <param name="variety"></param>
        /// <param name="sheepRescissionSex"></param>
        /// <param name="startAge"></param>
        /// <param name="endAge"></param>
        /// <returns></returns>
       public async Task<List<Breedingweigh>> BreedingWeighList(string startDate, string endDate, string earTag, int weighCategory, int variety, bool sheepRescissionSex, int startAge, int endAge)
        {

            var departs = context.Breedingweighs.Where(c => c.Isdelete == 0).AsQueryable().AsNoTracking();

            if (!string.IsNullOrEmpty(startDate))
            {
                 departs = departs.Where(c => c.WeighDate >= DateTime.Parse(startDate));
            }
            if(!string.IsNullOrEmpty(endDate))
            {
                 departs = departs.Where(c => c.WeighDate <= DateTime.Parse(endDate));
            }
            if(!string.IsNullOrEmpty(earTag))
            {
                departs = departs.Where(c => c.EarTag == earTag);
            }
            if(weighCategory>0)
            {
                departs = departs.Where(c => c.WeighCategory == weighCategory);
            }
            if (variety > 0)
            {
                departs = departs.Where(c => c.Variety == variety);
            }
            if (sheepRescissionSex)
            {
                departs = departs.Where(c => c.SheepRescissionSex == sheepRescissionSex);              
            }
            if(startAge>0)
            {
                departs = departs.Where(c => c.WeightMonthsAge >= startAge);
            }
            if (endAge > 0)
            {
                departs = departs.Where(c => c.WeightMonthsAge <= endAge);
            }

            return departs.ToList();
             
        }


        /// <summary>
        /// 删除称重数据
        /// </summary>
        /// <param name="breedingID"></param>
        /// <returns></returns>
        public async Task<int> Delete(int breedingID)
        {
            var sheep = context.Breedingweighs.Find(breedingID);
            sheep.Isdelete = 1;
            return await context.SaveChangesAsync();
        }



        /// <summary>
        /// 添加称重数据
        /// </summary>
        /// <param name="breedingweigh"></param>
        /// <returns></returns>
        public async Task<int> Add(Breedingweigh breedingweigh)
        {
            context.Breedingweighs.Add(breedingweigh);
            return await context.SaveChangesAsync();
        }


        /// <summary>
        /// 修改称重数据
        /// </summary>
        /// <param name="breedingweigh"></param>
        /// <returns></returns>
        public async Task<int> Update(Breedingweigh breedingweigh)
        {
            context.Breedingweighs.Update(breedingweigh);
            return await context.SaveChangesAsync();
        }






    }




}
